package com.ruoyi.his.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.his.domain.NewHisPrescription;
import com.ruoyi.his.service.INewHisPrescriptionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 处方药品Controller
 *
 * @author vvjh
 * @date 2025-04-02
 */
@RestController
@RequestMapping("/his/newprescription")
public class NewHisPrescriptionController extends BaseController
{
    @Autowired
    private INewHisPrescriptionService newHisPrescriptionService;

    /**
     * 查询处方药品列表
     */

    @GetMapping("/list")
    public TableDataInfo list(NewHisPrescription newHisPrescription)
    {
        startPage();
        List<NewHisPrescription> list = newHisPrescriptionService.selectNewHisPrescriptionList(newHisPrescription);
        return getDataTable(list);
    }

    /**
     * 导出处方药品列表
     */

    @Log(title = "处方药品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NewHisPrescription newHisPrescription)
    {
        List<NewHisPrescription> list = newHisPrescriptionService.selectNewHisPrescriptionList(newHisPrescription);
        ExcelUtil<NewHisPrescription> util = new ExcelUtil<NewHisPrescription>(NewHisPrescription.class);
        util.exportExcel(response, list, "处方药品数据");
    }

    /**
     * 获取处方药品详细信息
     */

    @GetMapping(value = "/{prescriptionId}")
    public AjaxResult getInfo(@PathVariable("prescriptionId") Long prescriptionId)
    {
        return AjaxResult.success(newHisPrescriptionService.selectNewHisPrescriptionByPrescriptionId(prescriptionId));
    }

    /**
     * 新增处方药品
     */

    @Log(title = "处方药品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NewHisPrescription newHisPrescription)
    {
        return toAjax(newHisPrescriptionService.insertNewHisPrescription(newHisPrescription));
    }

    /**
     * 修改处方药品
     */

    @Log(title = "处方药品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NewHisPrescription newHisPrescription)
    {
        return toAjax(newHisPrescriptionService.updateNewHisPrescription(newHisPrescription));
    }

    /**
     * 删除处方药品
     */

    @Log(title = "处方药品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{prescriptionIds}")
    public AjaxResult remove(@PathVariable Long[] prescriptionIds)
    {
        return toAjax(newHisPrescriptionService.deleteNewHisPrescriptionByPrescriptionIds(prescriptionIds));
    }

    //根据就诊ID查询处方List
    @GetMapping("/getListByTreatId/{treatId}")
    public AjaxResult getListByTreatId(@PathVariable String treatId)
    {
        return AjaxResult.success(newHisPrescriptionService.selectNewHisPrescriptionListByTreatId(treatId));
    }
}
